package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.employee.Department;
//import com.example.chcrm.service.DepartmentService;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.service.DepartmentService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;

/**
 * @Author 邹
 * @Date 2025/4/17 1:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/department")
@CrossOrigin // 允许跨域访问
public class DepartmentController {
    @Autowired
//    DepartmentService departmentService;
    DepartmentService departmentService;

    /**
     * 部门查询
     */
    @PostMapping("/pageList")
    public ResponseMsg<IPage<Department>> pageList(@RequestBody Department department) {
        HttpServletRequest request = ServletUtils.getRequest();
        Map<String, String>  map = ServletUtils.getHeaders(request);
        String empVoHeader=null;
        if (map !=null) {
        empVoHeader = map.get("empvo");
        }
        EmployeeVo empVo = new EmployeeVo();
        if (StringUtils.isNotBlank(empVoHeader)) {
            // 1. URL安全BASE64解码
            String base64 = empVoHeader.replace('-', '+').replace('_', '/');
            byte[] decodedBytes = Base64.getDecoder().decode(base64);
            // 2. UTF-8解码
            String empNm = new String(decodedBytes, StandardCharsets.UTF_8);
            empVo.setEmpNm(empNm);
        }
        return  departmentService.pageList(department);
    }

    /**
     * 部门新增
     */
    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody Department department) {

        return  departmentService.add(department);
    }

    /**
     * 部门删除
     */
    @GetMapping("/delete/{id}")
    public ResponseMsg<Object> delete(@PathVariable String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (id == null || id.equals("")) {
            msg.fail("删除失败ID不能为空");
            return msg;
        }
        return departmentService.delete(id);
    }

    /**
     * 部门禁用/启用
     */
    @PostMapping("/disable")
    public ResponseMsg<Object> disable(@RequestBody Department department) {

        return  departmentService.disable(department);
    }

    /**
     * 部门修改
     */
    @PostMapping("/update")
    public ResponseMsg<Object> update(@RequestBody Department department) {

        return  departmentService.update(department);
    }

    /**
     * 查询所有部门
     *
     * essEmployee
     * @return
     */
    @GetMapping("/selectAllDepartment")
    public ResponseMsg<Object> pageList() {
        return departmentService.selectAllDepartment();
    }
}
